<template>
  <div class="rule">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>流程管理</el-breadcrumb-item>
      <el-breadcrumb-item><a>流程规则</a></el-breadcrumb-item>
    </el-breadcrumb>
    <div class="rule-content">
      <div class="rule-top">
        <div class="rule-top-left">
          <el-button type="primary" size="small" @click="add"
            >新建流程规则</el-button
          >
          <span class="published">已发布</span>
          <span class="draft">草稿</span>
        </div>
        <div class="rule-top-right">
          <el-input
            placeholder="请输入名称或code检索"
            suffix-icon="el-icon-search"
            size="small"
            @keydown.native.enter="getList"
            v-model="searchInfo"
          ></el-input>
        </div>
      </div>
      <div class="rule-cards scroll-content">
        <rule-card
          v-for="item in ruleDetails"
          :ruleDetail="item"
          :key="item.id"
          @operateClick="operateClick"
        />
      </div>
    </div>
    <el-dialog
      :title="dialogType ? '重命名' : '新建流程规则'"
      :visible.sync="dialogVisible"
      custom-class="rule-dialog"
      :before-close="beforeClose"
      width="30%"
    >
      <el-form
        :model="ruleForm"
        label-position="top"
        :rules="rules"
        ref="elForm"
      >
        <el-form-item label="名称" prop="process_rule_name">
          <el-input
            v-model="ruleForm.process_rule_name"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="code" prop="process_rule_code">
          <el-input
            v-model="ruleForm.process_rule_code"
            size="small"
            :disabled="dialogType ? true : false"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input
            v-model="ruleForm.describe"
            type="textarea"
            resize="none"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="dialogSubmit" size="small">{{
          dialogType ? "确 定" : "新 建"
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ruleCard from "@/components/systemtools/ruleCard.vue";
import {
  addRule,
  changeProcessRule,
  deleteProcessRule,
  getAllProcessRule,
} from "@/http/api";
import dayjs from "dayjs";
export default {
  components: { ruleCard },
  name: "rule",
  data() {
    return {
      ruleDetails: [],
      dialogType: 0,
      dialogVisible: false,
      ruleForm: {
        process_rule_name: "",
        describe: "",
      },
      rules: {
        process_rule_name: [{ required: true, message: "请填写流程规则名称" }],
        process_rule_code: [{ required: true, message: "请填写规则code" }],
      },
      searchInfo: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    add() {
      this.dialogType = 0;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.elForm.clearValidate();
      });
    },
    async operateClick(rule, type) {
      switch (type) {
        case "reName":
          this.dialogType = 1;
          this.dialogVisible = true;
          this.ruleForm.process_rule_name = rule.process_rule_name;
          this.ruleForm.describe = rule.describe;
          this.ruleForm.process_rule_code = rule.process_rule_code;
          this.$nextTick(() => {
            this.$refs.elForm.clearValidate();
          });
          break;
        case "delete":
          let res = await deleteProcessRule(rule.process_rule_code);
          if (res.code === 20021) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
          break;
        case "view":
          this.$router.push({
            name: "ruleEdit",
            params: {
              process_rule_code: rule.process_rule_code,
              ifEdit: false,
            },
          });
        default:
          break;
      }
    },
    beforeClose() {
      this.dialogVisible = false;
      setTimeout(() => {
        this.ruleForm = {
          process_rule_name: "",
          describe: "",
          process_rule_code: "",
        };
      }, 200);
    },
    dialogSubmit() {
      this.$refs.elForm.validate(async (res) => {
        if (res) {
          let res = {};
          if (this.dialogType) {
            // 修改
            res = await changeProcessRule(this.ruleForm);
          } else {
            // 新建
            res = await addRule(this.ruleForm);
          }
          if (res.code === 20011 || res.code === 20031) {
            this.$message.success(res.code === 20011 ? "创建成功" : "修改成功");
            this.dialogVisible = false;
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        }
      });
    },
    async getList() {
      let res = await getAllProcessRule(this.searchInfo);
      if (res.code === 20041) {
        this.ruleDetails = res.data;
        this.ruleDetails.forEach((item) => {
          item.update_time = dayjs(item.update_time).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          item.create_time = dayjs(item.create_time).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        });
      }
    },
  },
};
</script>
<style lang="less" scoped>
.rule {
  .rule-top {
    padding: 10px 14px;
    background-color: #fff;
    border-radius: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .published,
    .draft {
      position: relative;
      padding-left: 16px;
      margin: 0 16px;
      &::before {
        content: "";
        width: 12px;
        height: 12px;
        border-radius: 50%;
        background-color: #5abb3c;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
      }
      &.draft {
        margin: 0;
        &::before {
          background-color: #bbbfc4;
        }
      }
    }
  }
  .rule-cards {
    margin-top: 20px;
    display: flex;
    height: calc(100vh - 220px);
    flex-wrap: wrap;
    .rule-card {
      width: 28.49%;
      height: 180px;
      margin-right: 20px;
      &:nth-child(3n) {
        margin-right: 0;
      }
    }
  }
}
</style>
<style lang="less">
.rule-dialog {
  border-radius: 10px;
  overflow: hidden;
  .el-dialog__header {
    padding: 20px;
    background-color: #4c78fc;
    color: #fff;
    .el-dialog__process_rule_name,
    .el-dialog__close,
    .el-dialog__title {
      color: #fff;
    }
  }
  .el-form-item__label {
    padding-bottom: 0;
    line-height: 26px;
  }
}
</style>
